package com.zhiche.lisa.oms.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.oms.dao.mapper.LocationMapper;
import com.zhiche.lisa.oms.dao.model.Location;
import com.zhiche.lisa.oms.service.ILocationService;
import com.zhiche.lisa.oms.service.util.MdmUtil;
import com.zhiche.lisa.oms.vo.LocationTypeVo;
import com.zhiche.lisa.oms.vo.LocationVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 各类地址信息 服务实现类
 * </p>
 *
 * @author qichao
 * @since 2018-06-18
 */
@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements ILocationService {

    private Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private MdmUtil mdmUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LocationVo insertOrUpdateLocation(String typeValue,
                                             String value,
                                             String label,
                                             String adCode,
                                             Long teanantId) throws BaseException {
        LocationVo locationVo = null;
        boolean blnRtn = false;

        if (StringUtils.isEmpty(typeValue)) {
            throw new BaseException(-1, "地址类型名称为空");
        }

        if (StringUtils.isEmpty(value)) {
            throw new BaseException(-1, "地址详情为空");
        }

        if (StringUtils.isEmpty(label)) {
            throw new BaseException(-1, "系统显示值为空");
        }

        LocationTypeVo locationTypeVo = mdmUtil.getLocationTypeByValue(typeValue);
        if (Objects.isNull(locationTypeVo)) {
            throw new BaseException(-1, "地址类型不存在");
        }

        EntityWrapper<Location> ew = new EntityWrapper<>();
        ew.eq("type_id", locationTypeVo.getId());
        ew.eq("value", value);
        ew.eq("tenant_id", teanantId);
        List<Location> locations = baseMapper.selectListNoTenant(ew);
        Location location = null;
        if (CollectionUtils.isEmpty(locations)) {
            location = new Location();
            location.setTypeId(locationTypeVo.getId());
            location.setValue(value);
            location.setLabel(label);
            location.setAdCode(adCode);
            location.setTenantId(teanantId);
            blnRtn = this.insert(location);
        } else {
            location = locations.get(0);
            location.setTypeId(locationTypeVo.getId());
            location.setValue(value);
            location.setLabel(label);
            location.setLabel(label);
            location.setAdCode(adCode);
            location.setTenantId(teanantId);
            blnRtn = this.updateById(location);
        }

        if (blnRtn) {
            locationVo = new LocationVo();
            BeanUtils.copyProperties(location, locationVo);
            locationVo.setTypeValue(typeValue);
        }

        return locationVo;
    }

    @Override
    public LocationVo getLocationByValue(String typeValue, String value) {
        LocationVo locationVo = null;
        if (StringUtils.isEmpty(typeValue)) {
            throw new BaseException(-1, "地址类型名称为空");
        }

        LocationTypeVo locationTypeVo = mdmUtil.getLocationTypeByValue(typeValue);
        logger.info("getLocationByValue ===> locationTypeVo:{}", JSON.toJSONString(locationTypeVo));
        if (Objects.isNull(locationTypeVo)) {
            throw new BaseException(-1, "不存在的地址类型");
        }

        Long typeId = locationTypeVo.getId();
        EntityWrapper<Location> locationEntityWrapper = new EntityWrapper<>();
        locationEntityWrapper.eq("type_id", typeId);
        locationEntityWrapper.eq("value", value);
        List<Location> locationList = baseMapper.selectList(locationEntityWrapper);
        if (!CollectionUtils.isEmpty(locationList)) {
            Location location = locationList.get(0);
            locationVo = new LocationVo();
            BeanUtils.copyProperties(location, locationVo);
            locationVo.setTypeValue(typeValue);
        }

        return locationVo;
    }

    @Override
    public LocationVo getLocationByValue(String typeValue, String value, Long teanantId) {
        LocationVo locationVo = null;
        if (StringUtils.isEmpty(typeValue)) {
            throw new BaseException(-1, "地址类型名称为空");
        }

        LocationTypeVo locationTypeVo = mdmUtil.getLocationTypeByValue(typeValue);
        logger.info("getLocationByValue ===> locationTypeVo===>Erp:{}", JSON.toJSONString(locationTypeVo));
        if (Objects.isNull(locationTypeVo)) {
            throw new BaseException(-1, "不存在的地址类型");
        }

        Long typeId = locationTypeVo.getId();
        EntityWrapper<Location> locationEntityWrapper = new EntityWrapper<>();
        locationEntityWrapper.eq("type_id", typeId);
        locationEntityWrapper.eq("value", value);
        locationEntityWrapper.eq("tenant_id", teanantId);
        List<Location> locationList = baseMapper.selectListNoTenant(locationEntityWrapper);
        if (!CollectionUtils.isEmpty(locationList)) {
            Location location = locationList.get(0);
            locationVo = new LocationVo();
            BeanUtils.copyProperties(location, locationVo);
            locationVo.setTypeValue(typeValue);
        }

        return locationVo;
    }

    @Override
    public LocationVo getLocationById(Long id) {
        LocationVo locationVo = null;
        Location location = this.selectById(id);

        if (!Objects.isNull(location)) {
            locationVo = new LocationVo();
            BeanUtils.copyProperties(location, locationVo);
        }
        if (!Objects.isNull(locationVo)) {
            LocationTypeVo locationTypeVo = mdmUtil.getLocationTypeById(locationVo.getTypeId());
            if (!Objects.isNull(locationTypeVo)) {
                locationVo.setTypeValue(locationTypeVo.getValue());
            }
        }

        return locationVo;
    }

    @Override
    public LocationVo getDriverLocationById(Long id) {
        LocationVo locationVo = null;
        Location location = baseMapper.getDriverLoactionNoTenant(id);
        if (!Objects.isNull(location)) {
            locationVo = new LocationVo();
            BeanUtils.copyProperties(location, locationVo);
        }
        if (!Objects.isNull(locationVo)) {
            LocationTypeVo locationTypeVo = mdmUtil.getLocationTypeById(locationVo.getTypeId());
            if (!Objects.isNull(locationTypeVo)) {
                locationVo.setTypeValue(locationTypeVo.getValue());
            }
        }

        return locationVo;
    }


}
